import pymysql
import xlwt
# 数据库操作
config = {
    "host": "1.117.167.225",
    "user": "root",
    "password": "Aa1234zxcv",
    "database": "qc_live",
    "charset": "utf8"
}
conn = pymysql.connect(**config)


# 查询表里所有数据
def find_all():
    cursor = conn.cursor()
    tab_list = []
    query = ('SELECT * FROM t_logininfo where uid = '+str(100749))
    cursor.execute(query)
    for (item) in cursor:
        print(item)
        my_map = {'uid': item[0], 'head':item[1]}
        tab_list.append(my_map)
    cursor.close()

    # 生成报表
    work_book = xlwt.Workbook(encoding='utf-8')
    sheet = work_book.add_sheet('sheet表名')
    sheet.write(0, 0, '用户ID')
    sheet.write(0, 1, '头像')
    for index in range(len(tab_list)):
        sheet.write(index + 1, 0, tab_list[index]['uid'])
        sheet.write(index + 1, 1, tab_list[index]['head'])
    # work_book.save('C:\\Users\\Administrator\\Desktop\\用户消费统计1.xls')
    conn.close()
    return tab_list


if __name__ == '__main__':
    print(find_all())
